import React from "react";
import ReactDOM from "react-dom";
import thunk from "redux-thunk";
import fetch from "isomorphic-fetch";
import {createStore,applyMiddleware } from "redux";
import fetchReduser from "./redusers/fetchReduser"

const store = createStore(fetchReduser,applyMiddleware(thunk));

const getData = postTitle =>(dispatch,getState)=>{
    console.log(postTitle);
    //ajax请求第一步,其实就是返回的status数据请求中
    dispatch({
        type:postTitle+"_START" //postTitle=FETCH_DATA
    })
    //第二步 ajax请求
    fetch('api/v2/movie/top250')
    .then(function(response){
        console.log(response.json());
    })

}

class SimpleComp extends React.Component{
    fetchData(){
        this.props.dispatch(getData('FETCH_DATA'))
    }

    render(){
        console.log(this.props);
        return (
            <div>
                <button onClick={this.fetchData.bind(this)}>请求数据</button>
            </div>
        )
    }
}
function renderIt(){
    ReactDOM.render(
        <SimpleComp data={store.getState()} dispatch={(arg)=>{store.dispatch(arg)}}/>,
        document.getElementById("app"));
}
renderIt();

store.subscribe(renderIt);
